<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Models\GoodsSpecsValue;
use App\Services\GoodsSpecsService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsSpecsValueController extends AdminController
{
    //保存
    public function save(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'integer|digits_between:1,20|min:1',
            'goods_specs_id' => 'required|integer|digits_between:1,20|min:1',
            'name' => 'required|string',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $putData = $request->except(['_method', '_token']);
        $id = $request->input('id');

        $flight = GoodsSpecsValue::updateOrCreate(
            ['id' => $id],
            $putData
        );
        return $this->success($flight->id);
    }

    //获取规格值数据
    public function  listAll(Request $request){
        $validator = Validator::make($request->all(), [
            'goods_specs_id' => 'required|integer|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $goods_specs_id = $request->input('goods_specs_id');
        $list = GoodsSpecsService::getInstance()->specsVallist($goods_specs_id);
        return $this->success($list);
    }
    //删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $expert = GoodsSpecsValue::find($request->id);
        $expert->delete();
        return $this->success();
    }
}
